package com.cms.recursoshumanos.ui.user.recibo;

import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.DrawStyle;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.Ui;
import net.rim.device.api.ui.XYEdges;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.SeparatorField;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.decor.BorderFactory;

import com.cms.recursoshumanos.cache.UserInfoCache;
import com.cms.recursoshumanos.component.StringListStyleButtonField;
import com.cms.recursoshumanos.component.base.CustomListElement;
import com.cms.recursoshumanos.constant.Constant;
import com.cms.recursoshumanos.container.ForegroundManager;
import com.cms.recursoshumanos.container.ListStyleButtonSetManager;
import com.cms.recursoshumanos.container.ListStyleButtonSetScrollManager;
import com.cms.recursoshumanos.container.TwoColumnFieldManager;
import com.cms.recursoshumanos.handler.ListaReciboScreenHandler;
import com.cms.recursoshumanos.object.Recibo;
import com.cms.recursoshumanos.ui.RecursosHumanosScreen;
import com.cms.recursoshumanos.utilities.Tool;



/**
 * A class extending the MainScreen class, which provides default standard
 * behavior for BlackBerry GUI applications.
 */
public final class DetailReciboScreen extends RecursosHumanosScreen implements FieldChangeListener
{
	public static DetailReciboScreen instance = null;
	
	
	private Recibo elRecibo;
	private ForegroundManager _foreground;
	
	HorizontalFieldManager hfm;
	CustomListElement lblMontoRecibo, lblFechaRecibo, 
						lblStatusRecibo;
	TwoColumnFieldManager twoColumnManager;
	StringListStyleButtonField listOpcionEnvioRecibo;
	LabelField lblOpcionesAdicionales,lblMensaje;
	Manager _content, _content2;//, _bodyWrapper;
	ListStyleButtonSetScrollManager buttonSet;
	
	
	public static DetailReciboScreen getInstance(Recibo elRecibo)
	{
		if (instance == null)
		{
			return new DetailReciboScreen(elRecibo);
		}
		else
		{
			return instance;
		}
	}
	public DetailReciboScreen(Recibo elRecibo) 
	{
		super(NO_VERTICAL_SCROLL | USE_ALL_HEIGHT);
		this.elRecibo = elRecibo;
		setComponents();
	}

	public void setListComponents()
	{
        Font font = getFont().derive(Font.PLAIN, 6, Ui.UNITS_pt);
        
		int width = font.getAdvance("Fecha de: ");
		
        lblMontoRecibo = new CustomListElement("Monto (BsF.)", ""+elRecibo.getMontoRecibo(), width) 
        {
			protected void paintBackground( Graphics g ) 
			{

				 
				  if( _drawPosition < 0 ) 
				  {
			          // it's like a list field, let the default background be drawn
			          super.paintBackground( g );
			          return;
			      }
			      
			      int oldColour = g.getColor();
			      int focus = Constant.COLOR_FOCUS_NORMAL;
			      if(_doAction)
			      {
			    	  focus = Constant.COLOR_LIST_FOCUS;
			      }
			      
			      int background = g.isDrawingStyleSet( Graphics.DRAWSTYLE_FOCUS ) ? focus : Constant.COLOR_UNFOCUS_NORMAL;
			      
			      try 
			      {
			    	  
			          if( _drawPosition == 0 ) 
			          {
			              // Top
			              g.setColor( background );
			              g.fillRoundRect( 0, 0, getWidth(), getHeight() + CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS );
			              g.setColor( COLOR_BORDER );
			              g.drawRoundRect( 0, 0, getWidth(), getHeight() + CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS );
			              g.drawLine( 0, getHeight() - 1, getWidth(), getHeight() - 1 );
			          } 
			          else if( _drawPosition == 1 ) 
			          {
			              // Bottom 
			              g.setColor( background );
			              g.fillRoundRect( 0, -CORNER_RADIUS, getWidth(), getHeight() + CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS );
			              g.setColor( COLOR_BORDER );
			              g.drawRoundRect( 0, -CORNER_RADIUS, getWidth(), getHeight() + CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS );
			          } 
			          else if( _drawPosition == 2 ) 
			          {
			              // Middle
			              g.setColor( background );
			              g.fillRoundRect( 0, -CORNER_RADIUS, getWidth(), getHeight() + 2 * CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS );
			              g.setColor( COLOR_BORDER );
			              g.drawRoundRect( 0, -CORNER_RADIUS, getWidth(), getHeight() + 2 * CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS );
			              g.drawLine( 0, getHeight() - 1, getWidth(), getHeight() - 1 );
			          } 
			          else 
			          {
			              // Single
			              g.setColor( background );
			              g.fillRoundRect( 0, 0, getWidth(), getHeight(), CORNER_RADIUS, CORNER_RADIUS );
			              g.setColor( COLOR_BORDER );
			              g.drawRoundRect( 0, 0, getWidth(), getHeight(), CORNER_RADIUS, CORNER_RADIUS );
			          }
			          g.drawLine(_preferedWidth+SEPARATOR_LONG, 0, _preferedWidth+SEPARATOR_LONG, getHeight());
			      } 
			      finally 
			      {
			          g.setColor( oldColour );
			      }
			  }
		}; 
		lblFechaRecibo = new CustomListElement("Fecha de Emision", 
				Tool.getInstance().parseDateToString(elRecibo.getFechaRecibo(), 6), width) 
		{
			protected void paintBackground( Graphics g ) 
			{
				

				  if( _drawPosition < 0 ) 
				  {
			          // it's like a list field, let the default background be drawn
			          super.paintBackground( g );
			          return;
			      }
			      
			      int oldColour = g.getColor();
			      int focus = Constant.COLOR_FOCUS_NORMAL;
			      if(_doAction) 
			    	  focus = Constant.COLOR_LIST_FOCUS;
			      
			      int background = g.isDrawingStyleSet( Graphics.DRAWSTYLE_FOCUS ) ? focus : Constant.COLOR_UNFOCUS_NORMAL;
			      
			      try 
			      {
			    	  
			          if( _drawPosition == 0 ) 
			          {
			              // Top
			              g.setColor( background );
			              g.fillRoundRect( 0, 0, getWidth(), getHeight() + CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS );
			              g.setColor( COLOR_BORDER );
			              g.drawRoundRect( 0, 0, getWidth(), getHeight() + CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS );
			              g.drawLine( 0, getHeight() - 1, getWidth(), getHeight() - 1 );
			          } 
			          else if( _drawPosition == 1 ) 
			          {
			              // Bottom 
			              g.setColor( background );
			              g.fillRoundRect( 0, -CORNER_RADIUS, getWidth(), getHeight() + CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS );
			              g.setColor( COLOR_BORDER );
			              g.drawRoundRect( 0, -CORNER_RADIUS, getWidth(), getHeight() + CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS );
			          } 
			          else if( _drawPosition == 2 ) 
			          {
			              // Middle
			              g.setColor( background );
			              g.fillRoundRect( 0, -CORNER_RADIUS, getWidth(), getHeight() + 2 * CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS );
			              g.setColor( COLOR_BORDER );
			              g.drawRoundRect( 0, -CORNER_RADIUS, getWidth(), getHeight() + 2 * CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS );
			              g.drawLine( 0, getHeight() - 1, getWidth(), getHeight() - 1 );
			          } 
			          else 
			          {
			              // Single
			              g.setColor( background );
			              g.fillRoundRect( 0, 0, getWidth(), getHeight(), CORNER_RADIUS, CORNER_RADIUS );
			              g.setColor( COLOR_BORDER );
			              g.drawRoundRect( 0, 0, getWidth(), getHeight(), CORNER_RADIUS, CORNER_RADIUS );
			          }
			          g.drawLine(_preferedWidth+SEPARATOR_LONG, 0, _preferedWidth+SEPARATOR_LONG, getHeight());
			      } 
			      finally 
			      {
			          g.setColor( oldColour );
			      }
			  }
		};
		lblStatusRecibo = new CustomListElement("Tipo", elRecibo.getTipoRecibo().getNombreTipoRecibo(), width) 
		{
			protected void paintBackground( Graphics g ) 
			{

				  if( _drawPosition < 0 ) 
				  {
			          // it's like a list field, let the default background be drawn
			          super.paintBackground( g );
			          return;
			      }
			      
			      int oldColour = g.getColor();
			      int focus = Constant.COLOR_FOCUS_NORMAL;
			      if(_doAction) 
			    	  focus = Constant.COLOR_LIST_FOCUS;
			      
			      int background = g.isDrawingStyleSet( Graphics.DRAWSTYLE_FOCUS ) ? focus : Constant.COLOR_UNFOCUS_NORMAL;
			      
			      try 
			      {
			    	  
			          if( _drawPosition == 0 ) 
			          {
			              // Top
			              g.setColor( background );
			              g.fillRoundRect( 0, 0, getWidth(), getHeight() + CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS );
			              g.setColor( COLOR_BORDER );
			              g.drawRoundRect( 0, 0, getWidth(), getHeight() + CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS );
			              g.drawLine( 0, getHeight() - 1, getWidth(), getHeight() - 1 );
			          } 
			          else if( _drawPosition == 1 ) 
			          {
			              // Bottom 
			              g.setColor( background );
			              g.fillRoundRect( 0, -CORNER_RADIUS, getWidth(), getHeight() + CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS );
			              g.setColor( COLOR_BORDER );
			              g.drawRoundRect( 0, -CORNER_RADIUS, getWidth(), getHeight() + CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS );
			          } 
			          else if( _drawPosition == 2 ) 
			          {
			              // Middle
			              g.setColor( background );
			              g.fillRoundRect( 0, -CORNER_RADIUS, getWidth(), getHeight() + 2 * CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS );
			              g.setColor( COLOR_BORDER );
			              g.drawRoundRect( 0, -CORNER_RADIUS, getWidth(), getHeight() + 2 * CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS );
			              g.drawLine( 0, getHeight() - 1, getWidth(), getHeight() - 1 );
			          } 
			          else 
			          {
			              // Single
			              g.setColor( background );
			              g.fillRoundRect( 0, 0, getWidth(), getHeight(), CORNER_RADIUS, CORNER_RADIUS );
			              g.setColor( COLOR_BORDER );
			              g.drawRoundRect( 0, 0, getWidth(), getHeight(), CORNER_RADIUS, CORNER_RADIUS );
			          }
			          g.drawLine(_preferedWidth+SEPARATOR_LONG, 0, _preferedWidth+SEPARATOR_LONG, getHeight());
			      } 
			      finally 
			      {
			          g.setColor( oldColour );
			      }
			  }
		};

		_content.add( lblFechaRecibo );
		_content.add( lblMontoRecibo );
        _content.add( lblStatusRecibo );       
        
        twoColumnManager.add(_content);
        

	}
	
	public void setComponents() 
	{
		setTitle("Detalle del Recibo");
		_foreground = new ForegroundManager();
		//_bodyWrapper = new NegativeMarginVerticalFieldManager(USE_ALL_WIDTH);
		
		twoColumnManager = new TwoColumnFieldManager( Display.getWidth() / 2, USE_ALL_WIDTH );
        twoColumnManager.setBorder( BorderFactory.createBitmapBorder( new XYEdges( 10, 10, 10, 10 ), Bitmap.getBitmapResource( "border_simple_white.png" ) ) );
        twoColumnManager.setMargin( 6, 6, 6, 6 );
        twoColumnManager.setPadding( 6, 2, 2, 2 );
        
        Field labelNombre;
        
        labelNombre = new LabelField( "Recibo Nro. " + this.elRecibo.getNumeroRecibo() , 
        								FIELD_HCENTER |
        								FIELD_VCENTER |
        								LabelField.USE_ALL_WIDTH |
        								DrawStyle.HCENTER |
        								DrawStyle.VCENTER);
        labelNombre.setFont(font_bold_8);
        
        twoColumnManager.add(labelNombre);
        
        lblMensaje = new LabelField("Informacion General", LabelField.HCENTER | FIELD_HCENTER);
        lblMensaje.setFont(font_italic_7);
		twoColumnManager.add ( lblMensaje );
		Field separator = new SeparatorField();
        separator.setMargin( 2, 0, 2, 0 );
        twoColumnManager.add( separator );

        _content = new ListStyleButtonSetManager();
        
        setListComponents();    
        
        lblOpcionesAdicionales = new LabelField("Opciones Adicionales", USE_ALL_WIDTH);
        lblOpcionesAdicionales.setFont(font_italic_7);
        twoColumnManager.add( lblOpcionesAdicionales );
        
        buttonSet = new ListStyleButtonSetScrollManager();
        buttonSet.setScreen(this);
        
        llenar_opciones_adicionales();
                
        _foreground.add(twoColumnManager);
        add(_foreground);
        
	}
	public void llenar_opciones_adicionales()
	{
		listOpcionEnvioRecibo = new StringListStyleButtonField(
													"Enviar por Correo el Recibo", 
													LabelField.ELLIPSIS) 
		{
			protected void paintBackground( Graphics g )
			{
				  COLOR_BACKGROUND_FOCUS = Constant.COLOR_LIST_FOCUS;
				  COLOR_BACKGROUND_FOCUS_ACTION = Constant.COLOR_FOCUS_NORMAL;

			      if( _drawPosition < 0 ) 
			      {
			          // it's like a list field, let the default background be drawn
			          super.paintBackground( g );
			          return;
			      }
			      
			      int oldColour = g.getColor();
			      
			      int focus = COLOR_BACKGROUND_FOCUS;
			      if(_doAction) 
			    	  focus = COLOR_BACKGROUND_FOCUS_ACTION;
			      
			      int background = g.isDrawingStyleSet( Graphics.DRAWSTYLE_FOCUS ) ? focus : COLOR_BACKGROUND;
			      try 
			      {
			          if( _drawPosition == 0 ) 
			          {
			              // Top
			              g.setColor( background );
			              g.fillRoundRect( 0, 0, getWidth(), getHeight() + CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS );
			              g.setColor( COLOR_BORDER );
			              g.drawRoundRect( 0, 0, getWidth(), getHeight() + CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS );
			              g.drawLine( 0, getHeight() - 1, getWidth(), getHeight() - 1 );
			          } 
			          else if( _drawPosition == 1 ) 
			          {
			              // Bottom 
			              g.setColor( background );
			              g.fillRoundRect( 0, -CORNER_RADIUS, getWidth(), getHeight() + CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS );
			              g.setColor( COLOR_BORDER );
			              g.drawRoundRect( 0, -CORNER_RADIUS, getWidth(), getHeight() + CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS );
			          } 
			          else if( _drawPosition == 2 ) 
			          {
			              // Middle
			              g.setColor( background );
			              g.fillRoundRect( 0, -CORNER_RADIUS, getWidth(), getHeight() + 2 * CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS );
			              g.setColor( COLOR_BORDER );
			              g.drawRoundRect( 0, -CORNER_RADIUS, getWidth(), getHeight() + 2 * CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS );
			              g.drawLine( 0, getHeight() - 1, getWidth(), getHeight() - 1 );
			          } 
			          else 
			          {
			              // Single
			              g.setColor( background );
			              g.fillRoundRect( 0, 0, getWidth(), getHeight(), CORNER_RADIUS, CORNER_RADIUS );
			              g.setColor( COLOR_BORDER );
			              g.drawRoundRect( 0, 0, getWidth(), getHeight(), CORNER_RADIUS, CORNER_RADIUS );
			          }
			      } 
			      finally 
			      {
			          g.setColor( oldColour );
			      }
			}
		};
		listOpcionEnvioRecibo.setChangeListener( this );
		
		
		buttonSet.add(listOpcionEnvioRecibo);
		
		twoColumnManager.add(buttonSet);
		
	}
	
	
	public void fieldChanged( Field field, int context ) 
	{
		switch (field.getIndex())
		{
			case 0: // Envio Recibo
					ListaReciboScreenHandler handler = new ListaReciboScreenHandler();
					handler.getPlanillaRecibo(UserInfoCache.getInstance().getUsuario().getNombre_Usuario(), 
												UserInfoCache.getInstance().getUsuario().getPassword(), 
												elRecibo);
				break;
			
		}
	}	

}
